<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>设备品牌</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
  <script type="text/javascript">
  //编写自定义JS代码
  </script>
 </head>
 <body>
  <t:formvalid formid="formobj" dialog="true" layout="table" action="studentAccountController.do?doCharge" tiptype="3">
		<input id="accountId" name="accountId" type="hidden" value="${account.id }"> 
		<table style="" cellpadding="0" cellspacing="1" class="formtable">
				<tr>
					<td align="right">
						<label class="Validform_label">
							用户名:
						</label>
					</td>
					<td class="value">
					     	${userName}
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							姓名:
						</label>
					</td>
					<td class="value">
					     	${realName}
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label" >
							账号余额:
						</label>
					</td>
					<td class="value" id='balance' style='font-size:16px'>
					      <c:choose>
					     	<c:when test='${account.balance == null}'>
					     		0
					     	</c:when>
					     	<c:otherwise>
					     		${account.balance }
					     	</c:otherwise>
					     </c:choose>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							充值金额:
						</label>
					</td>
					<td class="value">
					    <input id="chargeAmount" name="chargeAmount" type="text" style="width: 150px" class="inputxt">
						 
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							充值后余额:
						</label>
					</td>
					<td class="value" id='finalBlance'>
					     <c:choose>
					     	<c:when test='${account.balance == null}'>
					     		0
					     	</c:when>
					     	<c:otherwise>
					     		${account.balance }
					     	</c:otherwise>
					     </c:choose>
					</td>
				</tr>
				
			</table>
		</t:formvalid>
		<div style="text-align:center;margin-top:20px;">
			<button type="button" id="but_login"  onclick="doCharge()" class="btn-big">
                 	  充值
            </button>
 		</div>
 </body>
 <style>
 	#chargeAmount{
 		color: red;
    	font-size: 20px;
 	}
 </style>
 
 <script>
 	$(document).ready(function(){
 		$("#chargeAmount").keyup(function () {
            var reg = $(this).val().match(/^-?\d*\.?\d{0,2}/);
            var txt = '';
            if (reg != null) {
                txt = reg[0];
            }
            $(this).val(txt);
            
            changeFinalBalcne(txt);
            
        }).change(function () {
            $(this).keypress();
            var v = $(this).val();
            if (/\.$/.test(v))
            {
                $(this).val(v.substr(0, v.length - 1));
            }
        });
 	});
 	
 	function changeFinalBalcne(txt){
 		var balance = $('#balance').html().trim();
 		if(balance == '')balance = '0';
 		
 		var currentBalance = parseFloat(balance);
 		if(txt == '' || txt == '-'){
 	 		$("#finalBlance").html(currentBalance); 
 		}else{
 			var chargeAmcount = parseFloat(txt);
 	 		$("#finalBlance").html(currentBalance+chargeAmcount);	
 		}
 	}
 	
 	function doCharge(){
 		
 		var finalBlance = parseInt($("#finalBlance").html());
 		if(finalBlance < 0){
 			tip('账号余额不能小于0');
 			return;
 		}
 		
 		var chargeAmount = $('#chargeAmount').val();
 		if(chargeAmount == ''){
 			tip("请输入充值金额");
 			return;
 		}
 		chargeAmount = parseInt(chargeAmount);
 		if(chargeAmount == 0){
 			tip("充值金额不能为0");
 			return;
 		}
 		
 		$.ajax({
 			url:'studentAccountController.do?doCharge',
 			type:'post',
 			data:{
 				accountId:$('#accountId').val(),
 				chargeAmount:$('#chargeAmount').val()
 			},
 			success:function(d){
 				d = $.parseJSON(d);
 				tip(d.msg);
 				if(d.success){
 					$('#chargeAmount').val(0);
 					$('#balance').html($('#finalBlance').html());
 				}
 			}
 		});
 	}
 	
 	
 </script>
 